#include "backupque.h"
#include "ui_backupque.h"
#define BACKUP_MAX 105

backupQue::backupQue(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::backupQue)
{
    ui->setupUi(this);
    this->setWindowTitle("后备队列");

    ui->backupqueue->setContextMenuPolicy(Qt::CustomContextMenu);//增加右键菜单功能的控件
    ui->backupqueue->setSelectionBehavior(QAbstractItemView::SelectRows);//设置整行选择
    ui->backupqueue->setSelectionMode(QAbstractItemView::SingleSelection);//设置只选择一行

    //设置表格不可编辑
    ui->backupqueue->setEditTriggers(QAbstractItemView::NoEditTriggers);
    initQueue();
}

void backupQue::initQueue()//遍历后备队列，并显示
{

    foreach (PCB * p, backup)
    {
        int rowcount = ui->backupqueue->rowCount();
        ui->backupqueue->insertRow(rowcount);//增加一行
        ui->backupqueue->setItem(rowcount, 0, new QTableWidgetItem(p->getPID()));
        ui->backupqueue->setItem(rowcount, 1, new QTableWidgetItem(QString::number(p->getPriority())));
        ui->backupqueue->setItem(rowcount, 2, new QTableWidgetItem(QString::number(p->getRuntime())));
        ui->backupqueue->setItem(rowcount, 3, new QTableWidgetItem("后备"));
        ui->backupqueue->setItem(rowcount,4,new QTableWidgetItem(QString::number(p->getMemoryNeed())));
    }

//    qDebug() << "可用驱动：";
//    qDebug() << QSqlDatabase::drivers();
//    QSqlDatabase db;
//    if(QSqlDatabase::contains("backup"))
//    {
//        db = QSqlDatabase::database("backup");
//    }
//    else {
//        db = QSqlDatabase::addDatabase("QMYSQL", "backup");
//    }
//    db.setHostName("127.0.0.1");
//    db.setPort(3306);
//    db.setDatabaseName("cpuschedule");
//    db.setHostName("root");
//    db.setPassword("123456");

//    bool ok = db.open();

//    if(ok){

//        qDebug() << "database connect success";
//        QString sql = "SELECT pid,priority,runtime,status FROM backupque ORDER BY id ASC";
//        QSqlQuery result = db.exec(sql);
//        while(result.next())
//        {
//            int rowcount = ui->backupqueue->rowCount();
//            ui->backupqueue->insertRow(rowcount);//增加一行
//            ui->backupqueue->setItem(rowcount, 0, new QTableWidgetItem(result.value("pid").toString()));
//            ui->backupqueue->setItem(rowcount, 1, new QTableWidgetItem(result.value("priority").toString()));
//            ui->backupqueue->setItem(rowcount, 2, new QTableWidgetItem(result.value("runtime").toString()));
//            ui->backupqueue->setItem(rowcount, 3, new QTableWidgetItem(result.value("status").toString()));
//        }
//        db.close();
//    }
//    else{
//        qDebug()<<"database open failed";
//        QSqlError error;
//            error=db.lastError();
//            qDebug() << error;
//    }
}

backupQue::~backupQue()
{
    delete ui;
}

bool backupQue::addBackup(PCB * p)
{
    if(num_in_backup >= BACKUP_MAX) return false;

    PCB * pro = new PCB(p);
    backup.enqueue(pro);
    num_in_backup++;
    refreshAgain();
    return true;
}

PCB* backupQue::getHead()
{
   return backup.head();
}

void backupQue::delFirst()
{
    if(backup.isEmpty())
        return;
    backup.dequeue();
    num_in_backup--;
}

void backupQue::on_refresh_clicked()
{
    ui->backupqueue->setRowCount(0);
    ui->backupqueue->clearContents();
    initQueue();
}

void backupQue::refreshAgain()
{
    ui->backupqueue->setRowCount(0);
    ui->backupqueue->clearContents();
    initQueue();
}
